package view.macrogui
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import model.macro.Player;
	import model.macro.World;
	
	/**
	 */
	public class TopPanel extends Sprite
	{
		static public const HEIGHT:Number = 30;
		
		private const TITLE_FORMAT:TextFormat = new TextFormat("Helvetica", 16, 0xFFFFFF);
		private const GOLD_FORMAT:TextFormat = new TextFormat("Helvetica", 16, 0xFFFF44);
		private const YEAR_FORMAT:TextFormat = new TextFormat("Helvetica", 16, 0xFFFFFF);
		
		private var name_field:TextField;
		private var gold_field:TextField;
		private var year_field:TextField;
		private var next_turn_button:Sprite;
		private var research_button:Sprite;
		private var research_pane:ResearchPane;
		private var player:Player;
		private var world:World;
		
		/**
		 *
		 */
		public function TopPanel()
		{
			addEventListener(Event.ADDED_TO_STAGE, init)
		}
		
		/**
		 *
		 * @param	e
		 */
		private function init(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			name_field = new TextField();
			name_field.selectable = false;
			name_field.defaultTextFormat = TITLE_FORMAT;
			addChild(name_field);
			
			gold_field = new TextField();
			gold_field.selectable = false;
			gold_field.defaultTextFormat = GOLD_FORMAT;
			addChild(gold_field);
			
			year_field = new TextField();
			year_field.selectable = false;
			year_field.defaultTextFormat = YEAR_FORMAT;
			addChild(year_field);
			
			research_pane = new ResearchPane();
			addChild(research_pane);
			
			research_button = new Sprite();
			research_button.graphics.beginFill(0x33FF33);
			research_button.graphics.drawRoundRect(0, 0, 40, 24, 8);
			research_button.graphics.endFill();
			research_button.y = 3;
			research_button.addEventListener(MouseEvent.CLICK, function(e:Event):void
				{
					if (research_pane.opened)
						research_pane.close();
					else
						research_pane.open();
				});
			addChild(research_button);
			
			next_turn_button = new Sprite();
			next_turn_button.graphics.beginFill(0xCCCCCC);
			next_turn_button.graphics.drawRoundRect(0, 0, 40, 24, 8);
			next_turn_button.graphics.endFill();
			next_turn_button.addEventListener(MouseEvent.CLICK, function(e:Event):void
				{
					world.nextTurn();
				});
			next_turn_button.y = 3;
			addChild(next_turn_button);
			
			name_field.x = 10;
			gold_field.x = 100;
			year_field.x = 150;
			research_button.x = 200;
			next_turn_button.x = stage.stageWidth - 50;
			
			redraw();
		}
		
		/**
		 *
		 */
		public function redraw():void
		{
			graphics.clear();
			graphics.beginFill(0x777777, 0.8);
			graphics.drawRect(0, 0, stage.stageWidth, HEIGHT);
			graphics.endFill();
			
			name_field.text = player.name;
			gold_field.text = "" + player.gold;
			year_field.text = "" + world.turn;
		}
		
		public function update(currentPlayer:Player, world:World):void
		{
			this.world = world;
			this.player = currentPlayer;
		
		}
	
	}

}